﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace iHub
{
    public partial class login : Form
    {
        public string loggedUsername;
        public static login log = null;
        public static Boolean locked = false;
        private Boolean notterminated = false;
        private int numdump = 0;
        private int timer = 0;
        private AccountManager acct = new AccountManager();

        public login()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);
        }

        public string logGetUsername()
        {
            return loggedUsername;
        }


        private void login_Load(object sender, EventArgs e)
        {

        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            String user = username.Text;
            String pass = password.Text;
            Boolean type = false;
            DateTime date = DateTime.Now;
            String dt = date.ToShortDateString();
            String time = date.ToShortTimeString();
            Account a = new Account();
            Boolean allowed = false;

            notterminated = acct.isTerminated(user);

            a.setUsername(user);
            a.setPass(pass);

            allowed = acct.verifyLogin(a);

            numdump = acct.getLockdownCount();

            if (allowed && notterminated)
            {
                DialogResult msg = MessageBox.Show("You are now logged in as " + user + ". Please make sure to log out after use to prevent unauthorized access.", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                acct.resetCounter();
                loggedUsername = username.Text;

                username.Text = "";
                password.Text = "";
                log = this;
                this.Hide();

                acct.setAccount(a);
                type = a.getUsertype();
                acct.updateLoginHistory(user, type, date);
                main view = new main(a, dt, time);
                view.ShowDialog();
            }
            else if (!allowed || !notterminated)
            {
                if ((numdump + 1) % 3 != 0 || locked == true || numdump == 0)
                {
                    if (!allowed)
                    {
                        DialogResult msg = MessageBox.Show("Invalid username or password. ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    else
                    {
                        MessageBox.Show("This account has been terminated, please contact an administrator to reactivate this account ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }

                    username.Text = "";
                    password.Text = "";
                    acct.incLockdownCount();
                    locked = false;
                }
                else if ((numdump + 1) % 3 == 0 || locked == false)
                {
                    DialogResult lockmsg = MessageBox.Show("System will now go into lockdown mode due to consecutively entering invalid credentials", "Lockdown Mode", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    if (lockmsg == DialogResult.OK)
                    {
                        acct.incLockdownCount();
                        numdump = acct.getLockdownCount();
                        timer = 120 * (numdump / 3);
                        log = this;
                        this.Hide();
                        lockdown ldown = new lockdown(timer);
                        ldown.Show();
                    }
                    else
                    {
                        acct.incLockdownCount();
                        numdump = acct.getLockdownCount();
                        timer = 120 * (numdump / 3);
                        log = this;
                        this.Hide();
                        lockdown ldown = new lockdown(timer);
                        ldown.Show();
                    }
                }
            }
        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Application.Exit();
            }
        }

        private void forget_Click(object sender, EventArgs e)
        {
            this.Dispose();

            forgetStart view = new forgetStart();
            view.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Programmed by: GAEL Productions\n © 2011", "GAEL © 2011", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
